use PlPN qw(PN);
use strict;

=pod

Evals the selected text as Perl code and puts the result in the output
window.

=cut

PlPN->RegisterScript('example', 'Eval', sub {
	my $doc = PN->GetCurrentDocument;
	
	# SCI_GETSELTEXT = 2161;
	my $length = $doc->SendEditorMessage(2161, 0, undef);
	
	# make sure the buffer is big enough
	my $text = "\0" x $length;
	
	# SCI_GETSELTEXT = 2161;
	$doc->SendEditorMessage(2161, $length, $text);
	
	# text is returned with two trailing nulls, one from Scintilla
	# and one that XS adds on input to make sure the string is null-terminated
	$text=~s/\0+$//;
	
	my $result = eval $text;
	
	PN->GetGlobalOutputWindow->AddToolOutput("$text => $result\n");
	PN->GetGlobalOutputWindow->ShowOutput;
});
